package com.gmail.hh.flickrj.sample.android.fragments;


import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.gmail.hh.flickrj.sample.android.FlickrjMainActivity;
import com.gmail.hh.flickrj.sample.android.R;


@SuppressLint({ "ResourceAsColor", "NewApi" })
public class FragmentMenu extends Fragment  implements OnClickListener {
	
	public static final String TAG = "fragment_menu";


	private static final Logger logger = LoggerFactory.getLogger(FlickrjMainActivity.class);
	
	private View mView;
	LinearLayout menuMainLayout; 
	LinearLayout menuSubFor02Layout; 

	private static int maxMenuIdMain = 9;
	private static int maxMenuId = 99;
	public static int idMainUpload = 1;
	public static int idMainGallery = 2;
	public static int idMainContats = 3;
	public static int idSubGalleryList = 21;
	public static int idSubGalleryGrid = 22;
	public static int idSubGalleryMap = 23;
	public static int idSubGallerySlide = 24;
	
	private ImageButton[] buttons = new ImageButton[maxMenuId+1];
	private Integer[] selectedSub = new Integer[maxMenuId+1];
	private Integer lazySetSelectedItem = null;
	
	// each Activity which uses this Fragment must implement this interface
	IMenuSelectedListener mCallbackActivity;
	public interface IMenuSelectedListener {
		public void onMenuSelected(int position);
	}


	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
        	mCallbackActivity = (IMenuSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement IMenuSelectedListener");
        }
	}
	
	
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setRetainInstance(true);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
		// Inflate the  layout file.
		mView = inflater.inflate(R.layout.fragment_menu, null);	

		menuMainLayout = (LinearLayout) mView.findViewById(R.id.menuMain);
		menuSubFor02Layout = (LinearLayout) mView.findViewById(R.id.menuSubFor02);
		
		ImageButton menu = null;
		// menu1
		menu = (ImageButton) mView.findViewById(R.id.menuMain1);
		buttons[idMainUpload] = menu;
		selectedSub[idMainUpload] = null;
		// menu2
		menu = (ImageButton) mView.findViewById(R.id.menuMain2);
		buttons[idMainGallery] = menu;
		selectedSub[idMainGallery] = idSubGalleryGrid;
		// menu3
		menu = (ImageButton) mView.findViewById(R.id.menuMain3);
		buttons[idMainContats] = menu;
		selectedSub[idMainContats] = null;
		
		// menu21
		menu = (ImageButton) mView.findViewById(R.id.menuSub21);
		buttons[idSubGalleryList] = menu;
		// menu22
		menu = (ImageButton) mView.findViewById(R.id.menuSub22);
		buttons[idSubGalleryGrid] = menu;
		// menu23
		menu = (ImageButton) mView.findViewById(R.id.menuSub23);
		buttons[idSubGalleryMap] = menu;
		// menu24
		menu = (ImageButton) mView.findViewById(R.id.menuSub24);
		buttons[idSubGallerySlide] = menu;
		
		for(ImageButton button : buttons) {
			if (button!=null ) {
				button.setOnClickListener(this);
			}
		}

		if (lazySetSelectedItem!=null ) {
			setSelectedItem( lazySetSelectedItem.intValue() );
		}
		
		// return the view
		return mView;
	}

	@Override
	public void onViewCreated(View arg0, Bundle arg1) {
		super.onViewCreated(arg0, arg1);
	}

	@Override
	public void onClick(View v) {
		// find the clicked view (Menu-Item) in the Array
		for (int i=0; i<=maxMenuId; i++ ) {
			if (v==buttons[i])  {
				mCallbackActivity.onMenuSelected(i);
			}
		}
	}


	// used for external access
	public void setSelectedItem(int position) {
		if (position>0  && position<=maxMenuId ) {
			if (buttons[position]!=null ) {
				// reset background color of all ImageButtons and sub-menu
				menuSubFor02Layout.setVisibility( View.GONE ); // not space used when gone
				for (int i=0; i<=maxMenuId; i++ ) {
					if (buttons[i]!=null ) {
						buttons[i].setSelected(false);
					}
				}
				// set the selected menu
				buttons[position].setSelected(true);  
				
				logger.error("Selected position: " + position );
			      
				// a main-button was selected
				if (position<=maxMenuIdMain ) {
					Integer subPosition = selectedSub[position];
					if (subPosition!=null ) {
						// call recursively (indirect Recursion) 
						mCallbackActivity.onMenuSelected(subPosition);
					}
				}
				// a sub-button was selected
				else {
					int mainPosition = position/10; // the first digit
					selectedSub[mainPosition] = position;
					buttons[mainPosition].setSelected(true);
					if (mainPosition==idMainGallery ) {
						menuSubFor02Layout.setVisibility( View.VISIBLE );
					}
				}
			}
			// used when this method is called before onCreate was executed
			else {
				lazySetSelectedItem = Integer.valueOf(position);
			}
		}
	}




}
